package com.kye.express.business.waybill_take_photo;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.text.TextUtils;

import com.kye.base.Logger;
import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.express.utils.ImageUtils;
import com.kye.express.utils.StringUtils;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.online.GetUnpaidFreightListResultBean;
import com.kye.model.bean.online.GetWaybillPayMoneyResultBean;
import com.kye.model.bean.online.request.RequestGetWaybillPayMoneyBean;
import com.kye.model.bean.user.User;
import com.kye.model.bean.waybill_take_photo.RequestUploadWaybillPictureBean;
import com.kye.model.bean.waybill_take_photo.UploadWaybillPictureResultBean;
import com.kye.model.bean.waybill_take_photo.WaybillPhotograhbean;
import com.kye.model.service.OnlinePayService;
import com.kye.model.service.WaybillTakePhotoService;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface WaybillTakePhotoActivityContract {

    interface WaybillTakePhotoActivityView extends RxView {

        void onComplete();

        void onLoading();

        void onLoaded(List<UploadWaybillPictureResultBean> uploadWaybillPictureResultBeans);

        void onOtherError(String msg);

        void onApiError(String msg);

        void onAddWallbillButtonClicked();

    }

    class WaybillTakePhotoActivityViewPresenter extends BasePresenter<WaybillTakePhotoActivityContract.WaybillTakePhotoActivityView> {

        private Context mContext;

        public WaybillTakePhotoActivityViewPresenter(Context context) {
            this.mContext = context;
        }


        public void uploadWaybills(List<WaybillPhotograhbean> waybillPhotograhbeans) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            String mobileType = android.os.Build.MODEL;//手机型号
            if (StringUtils.isNullOrEmpty(mobileType)) {
                mobileType = Build.MANUFACTURER;//手机品牌
            }
            if (StringUtils.isNullOrEmpty(mobileType)) {
                mobileType = "Android";
            }

            RequestUploadWaybillPictureBean requestUploadWaybillPictureBean = new RequestUploadWaybillPictureBean();
            requestUploadWaybillPictureBean.setPhone(phone);
            List<RequestUploadWaybillPictureBean.ArrayBean> array = new ArrayList<>();
            for (WaybillPhotograhbean item : waybillPhotograhbeans) {
                if (TextUtils.isEmpty(item.getImagePath())){
                    continue;
                }
                File file = new File(item.getImagePath());
                double fileSize = file.length();
                double bitmapSize = fileSize / 1024;
                DecimalFormat df = new DecimalFormat("######0.00");
                String size = df.format(bitmapSize);//图片大小 KB

                RequestUploadWaybillPictureBean.ArrayBean arrayBean = new RequestUploadWaybillPictureBean.ArrayBean();
                arrayBean.setModel(mobileType);
                String picture = ImageUtils.bitmaptoString(BitmapFactory.decodeFile(item.getImagePath()));
                arrayBean.setPicture(picture);
                arrayBean.setPictureSize(size);
                arrayBean.setWaybill(item.getWaybillNumber());

                array.add(arrayBean);
            }
            requestUploadWaybillPictureBean.setArray(array);

            RetrofitManager.getInstance().createOpenApiService(WaybillTakePhotoService.class)
                    .uploadWaybillPicture(requestUploadWaybillPictureBean)
                    .compose(getView().<OpenApiResult<List<UploadWaybillPictureResultBean>>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<List<UploadWaybillPictureResultBean>>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onComplete();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<List<UploadWaybillPictureResultBean>> result, List<UploadWaybillPictureResultBean> datas) {
                            if (datas != null && datas.size() > 0) {
                                getView().onLoaded(datas);
                            }else {
                                getView().onOtherError(mContext.getString(R.string.common_upload_failed));
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<List<UploadWaybillPictureResultBean>> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onApiError(msg);
                            } else {
                                getView().onOtherError(mContext.getString(R.string.common_net_error));
                            }
                        }

                    });
        }
    }
}
